package com.leo;

import com.leo.bean.Student;
import com.leo.corn.job.CronJob;
import com.leo.simple.job.StudentJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.TimeZone;

/**
 * @ClassName CronJobTest
 * @Description
 * @Author wangss
 * @date 2021.07.15 10:12
 * @Version 1.0
 */
public class CronJobTest {

    public static void main(String[] args) throws SchedulerException {
        // 创建schedule实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.getContext().put("scheduler", "schedulerValue");

        // 创建JOb实例
        JobDetail j1 = JobBuilder.newJob(CronJob.class).withIdentity("j1", "g1").build();
        // 创建触发器，每隔2分钟，每天上午8点至下午5点之间
        Trigger t1 = TriggerBuilder.newTrigger().withIdentity("t1", "g1")
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 8-17 * * ?"))
                .build();

        JobDetail j2 = JobBuilder.newJob(CronJob.class).withIdentity("j2", "g1").build();
        // 创建触发器，将在上午10:42每天发射
        Trigger t2 = TriggerBuilder.newTrigger().withIdentity("t2", "g1")
                .withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(10, 42))
                .build();

        // 触发器，将在星期四上午10:39在TimeZone（系统默认值）之外触发
        JobDetail j3 = JobBuilder.newJob(CronJob.class).withIdentity("j3", "g1").build();
        Trigger t3 = TriggerBuilder.newTrigger().withIdentity("t3", "g1")
                .withSchedule(CronScheduleBuilder.weeklyOnDayAndHourAndMinute(DateBuilder.THURSDAY, 10, 39)
                        .inTimeZone(TimeZone.getTimeZone("America/Los_Angeles")))
                        .build();

        // 加入scheduler
        scheduler.scheduleJob(j1, t1);
        scheduler.scheduleJob(j2, t2);
        scheduler.scheduleJob(j3, t3);

        scheduler.start();
    }
}
